﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FlyBird
{
    //对象池
    class Barrierpool
    {
        //用于管理池中所有对象的容器
        private static List<Barrier> pool = new List<Barrier>();

        //池中初始对象的个数
        public static int initCount = 16;
        //对象池中最大个数
        public static int maxCount = 20;

        static Barrierpool()
        { 
            //初始化池中对象
            for (int i = 0; i < initCount; i++)
            {
                pool.Add(new Barrier());
            }
        }


        //从池中获取对象
        public static Barrier getPool()
        {
            int size = pool.Count;
            if (size > 0)
            {
                //移除并返回数据
                Barrier barrier = pool[size - 1];
                pool.RemoveAt(size - 1);
                return barrier;
            }
            else
            {
                //如果池中没有对象，只能new
                return new Barrier();
            }
        }

        //将对象归还容器
        public static void setPool(Barrier barrier)
        {
            if (pool.Count < maxCount)
            {
                pool.Add(barrier);
            }
        }
    }
}
